﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using AutoMapper;
using Huirui.Cavan.Application.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Domain.MainBoundedContext.PurchaseModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.PurchaseModule;
using Huirui.Cavan.Domain.Seedwork;

namespace Huirui.Cavan.Presentation.Controller.PurchaseModule
{
    public class UnPayController : EntityController<UnPay, long, UnPaySearch, IUnPayViewModel, IUnPayAppService>, IUnPayController
    {
        public override string ControllerName
        {
            get { return ControllerConst.UNPAY; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public UnPayController()
        {
            this.Model = ModelFactory.CreateModel<IUnPayViewModel>();
        }

        [ActionName(ActionConst.LIST)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "unpay_query")]
        public JsonResult GetUnPays(PayViewModel model)
        {
            UnPayJsonViewModel json = new UnPayJsonViewModel();
            json.rows = new List<UnPayJson>();

            IList<UnPay> unPays = this.AppService.GetBySearch(SearchFactory.CreateUnPaySearch(model.UnPaySearch.StartDate,model.UnPaySearch.EndDate, model.UnPaySearch.UnPayCode, model.UnPaySearch.SupplierCode)).ToList();
            foreach (UnPay unp in unPays)
            {
                UnPayJson unpj = Mapper.Map<UnPay, UnPayJson>(unp);
                json.rows.Add(unpj);
            }
            json.total = unPays.Count;

            return Json(json, "application/json");
        }

        [ActionName("listbytype")]
        [ActionPermissionAttribute(Name = "unpay_query")]
        public JsonResult GetUnPays(int type)
        {
            UnPayJsonViewModel json = new UnPayJsonViewModel();
            json.rows = new List<UnPayJson>();

            IList<UnPay> unPays = this.AppService.GetBySearch(SearchFactory.CreateUnPaySearch(type, string.Empty)).ToList();
            foreach (UnPay unp in unPays)
            {
                UnPayJson unpj = Mapper.Map<UnPay, UnPayJson>(unp);
                json.rows.Add(unpj);
            }
            json.total = unPays.Count;

            return Json(json, "application/json");
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "unpay_edit")]
        public ActionResult AddSupplierInitialBalance()
        {
            return View("~/Views/System/v1/addSupplierInitialBalance.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "unpay_edit")]
        public JsonResult AddSupplierInitialBalance(UnPayViewModel model)
        {
            BoolResult jsonResult = null;

            model.Item.UnPayType = (PurchaseIdentifier.UnPayType)model.Type;
            BoolGenericResult<UnPay> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
}
